import store from '@/store'

async function checkPermission(el, binding) {
  const { value } = binding
  if (value && value instanceof Array) {
    const centralConfig = store.getters.centralConfig
    const hasPermission = centralConfig && centralConfig.auth.grantedPolicies[value]
    if (!hasPermission) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  } else {
    throw new Error(`need roles! Like v-permission="['Logistics.Waybill','AbpIdentity.OrganizationUnits.Update']"`)
  }
}

export default {
  async inserted(el, binding) {
    await checkPermission(el, binding)
  },
  async update(el, binding) {
    await checkPermission(el, binding)
  }
}
